package com.zhny.project.txt.history.service.impl;

import com.zhny.common.utils.DateUtils;
import com.zhny.common.utils.text.Convert;
import com.zhny.project.txt.dataset.domain.TxtDataset;
import com.zhny.project.txt.dataset.mapper.TxtDatasetMapper;
import com.zhny.project.txt.history.domain.TxtHistory;
import com.zhny.project.txt.history.mapper.TxtHistoryMapper;
import com.zhny.project.txt.history.service.ITxtHistoryService;
import com.zhny.project.txt.indexset.domain.TxtIndexset;
import com.zhny.project.txt.indexset.mapper.TxtIndexsetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * TXT处理记录Service业务层处理
 *
 * @author yanqun
 * @date 2019-12-11
 */
@Service
public class TxtHistoryServiceImpl implements ITxtHistoryService
{


    @Autowired
    private TxtHistoryMapper txtHistoryMapper;
    @Autowired
    private TxtIndexsetMapper txtIndexsetMapper;
    @Autowired
    private TxtDatasetMapper txtDatasetMapper;
    /**
     * 查询TXT处理记录
     *
     * @param historyKey TXT处理记录ID
     * @return TXT处理记录
     */
    @Override
    public TxtHistory selectTxtHistoryById(String historyKey)
    {
        return txtHistoryMapper.selectTxtHistoryById(historyKey);
    }

    /**
     * 查询TXT处理记录列表
     *
     * @param txtHistory TXT处理记录
     * @return TXT处理记录
     */
    @Override
    public List<TxtHistory> selectTxtHistoryList(TxtHistory txtHistory)
    {
        return txtHistoryMapper.selectTxtHistoryList(txtHistory);
    }

    /**
     * 新增TXT处理记录
     *
     * @param txtHistory TXT处理记录
     * @return 结果
     */
    @Override
    public int insertTxtHistory(TxtHistory txtHistory)
    {
        txtHistory.setCreateTime(DateUtils.getNowDate());
        return txtHistoryMapper.insertTxtHistory(txtHistory);
    }

    /**
     * 修改TXT处理记录
     *
     * @param txtHistory TXT处理记录
     * @return 结果
     */
    @Override
    public int updateTxtHistory(TxtHistory txtHistory)
    {
        txtHistory.setUpdateTime(DateUtils.getNowDate());
        return txtHistoryMapper.updateTxtHistory(txtHistory);
    }

    /**
     * 删除TXT处理记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTxtHistoryByIds(String ids)
    {
        return txtHistoryMapper.deleteTxtHistoryByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除TXT处理记录信息
     *
     * @param historyKey TXT处理记录ID
     * @return 结果
     */
    public int deleteTxtHistoryById(String historyKey)
    {
        return txtHistoryMapper.deleteTxtHistoryById(historyKey);
    }


    //查询
    public List<TxtIndexset> deletedataset(int flag){
        List<TxtIndexset> txtIndexsetList = txtDatasetMapper.txtIndexsetList(flag);
        return txtIndexsetList;
    }

    //查询
    @Transactional
    public int TxtHistorydele(TxtHistory txtHistory){

            int mun = 0;
        if (txtHistory.getDatasetKey().equals("0")){

        }else {
            //查找数据表名称
            TxtDataset txtDataset = txtDatasetMapper.getSurfaceName(txtHistory.getDatasetKey());
//        DROP TABLE table_name ;
            //删除数据表
            StringBuffer sql = new StringBuffer();
            sql.append(" DROP TABLE " + txtDataset.getSurfaceName() + " ;");
            int Result = txtHistoryMapper.txtHistorydele(sql.toString());
            if (Result >= 0){
                mun = 1;
            }else {
                return 0;
            }

            //删除记录
            int txtHistoryById = txtHistoryMapper.deleteTxtHistoryById(txtHistory.getDatasetKey());
            if (txtHistoryById>0){
                mun = 1;
            }else {
                return 0;
            }

            //删除指标集数据
            int txtdele = txtIndexsetMapper.deletesTxtHistory(txtHistory.getDatasetKey());
            if (txtdele>0){
                mun = 1;
            }else {
                return 0;
            }
            //删除数据集数据
            int indexset = txtDatasetMapper.deletesTxtHistory(txtHistory.getDatasetKey());
            if (indexset>0){
                mun = 1;
            }else {
                return 0;
            }

        }

        return mun;
    }

    //恢复保存
    @Transactional
    public int txtrecovery(TxtHistory txtHistory){
        int mun = 0;
        //查找数据表名称
        TxtDataset txtDataset = txtDatasetMapper.getSurfaceName(txtHistory.getDatasetKey());
//        DROP TABLE table_name ;
        //删除数据表
        StringBuffer sql = new StringBuffer();
//        update base_time set delete_flag=1 where time_key = #{timekey}
        sql.append(" update " + txtDataset.getSurfaceName());
        sql.append(" set delete_flag=0 where dataset_key = '");
        sql.append(txtHistory.getDatasetKey() + "'");
        int Result = txtHistoryMapper.updateHistorydele(sql.toString());
        if (Result > 0){
            mun = 1;
        }else {
            return 0;
        }
        //恢复指标集
        int txtdele = txtIndexsetMapper.updateHistorydele(txtHistory.getDatasetKey());
        if (txtdele > 0){
            mun = 1;
        }else {
            return 0;
        }
        //恢复数据集
        int indexset = txtDatasetMapper.updateHistorydele(txtHistory.getDatasetKey());
        if (indexset > 0){
            mun = 1;
        }else {
            return 0;
        }

        return mun;
    }
}
